<?php

class PermissionController extends Zend_Controller_Action
{

    public function init()
    {
        parent::init();

        if (Zend_Auth::getInstance()->hasIdentity())
        {
            $user = Zend_Auth::getInstance()->getIdentity();

            $this->view->userProfile = $user->profile;
        }
        else
            $this->_redirect('/index');
    }

    public function indexAction()
    {
        $this->view->friends = Friendship::findAllFriends();
        $this->view->groups = UserGroup::findAll();
    }

    public function updateAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        try
        {
            $conn = Doctrine_Manager::connection();
            $conn->beginTransaction();

            $groups = UserGroup::findAll();

            foreach ($groups as $group)
            {
                $permission = $this->_getParam($group->id);

                $groupPermission = GroupPermission::findPermissionByGroup($group->id);
                $groupPermission->permission = $permission;
                $groupPermission->save();
            }

            $conn->commit();

            $this->_redirect('permission/index');
            
        } catch (Exception $e)
        {
            $conn->rollback();

            echo $e->getMessage();
        }
    }

}